﻿using Dtos;
using IBLL;
using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class DepartmentBll : IDepartmentBll
    {
        private IDepartmentDal _dal;
        public DepartmentBll(IDepartmentDal dal)
        {
            _dal = dal;
        }
        public async Task<int> AddDepartmentAsync(string title)
        {
            return await _dal.AddAsync(new Model.Department { Title = title});
        }

        public async Task<int> DeleteDepartmenAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return data != null ? await _dal.DeleteAsync(data) : 0;
        }

        public async Task<List<DepartmentDto>> GetAll()
        {
            var data = await _dal.GetAll();
            return data.Select(d => new DepartmentDto 
            {
                Id = d.Id,
                Title = d.Title,
                DeleteId = d.DeleteId,
                CreateTime = d.CreateTime,
                UpdateTime = d.UpdateTime,
            }).ToList();
        }

        public async Task<DepartmentDto> GetDepartmenByIdAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return new DepartmentDto
            {
                Id = data.Id,
                Title = data.Title,
            };
        }

        public Task<List<DepartmentDto>> GetDepartmenByNameAsync(string title)
        {
            throw new NotImplementedException();
        }

        public async Task<int> UpdateDepartmenAsync(Guid id, string title, DateTime updateTime)
        {
            var data = await _dal.GetById(id);
            if (data == null)
                return 0;
            data.Title = title;
            data.UpdateTime = updateTime;
            return await _dal.UpdateAsync(data);
        }
    }

}
